<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible=true">添加分类</el-button>
      </div>
      <!-- 文章分类表格 -->
      <!-- data:表格要渲染数据(数组) -->
       <el-table :data="cateList" style="width: 100%" stripe border>
        <el-table-column label="序号" type='index' width="180"></el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"></el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
        <el-table-column label="操作">
            <template v-slot="{row}">
                <el-button type="primary" size="mini" @click="editCate(row.id)">修改</el-button>
                <el-button type="danger" size="mini" @click="delCate(row.id)">删除</el-button>
            </template>
        </el-table-column>
        </el-table>
    </el-card>

    <!-- 添加分类弹出框 -->
    <el-dialog
        title="添加文章分类"
        :visible.sync="addVisible"
        width="35%"
        @close="handleAddClose">
        <!-- 添加分类的表单 -->
        <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="80px">
            <el-form-item prop="cate_name" label="分类名称" >
                <el-input v-model="addCateForm.cate_name" minlength="1" maxlength="10"></el-input>
            </el-form-item>
            <el-form-item prop="cate_alias" label="分类别名">
                <el-input v-model="addCateForm.cate_alias" minlength="1" maxlength="15"></el-input>
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button @click="addVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
    </el-dialog>

    <!-- 修改文章分类的对话框 -->
    <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%" @close="handleEditClose">

      <el-form :model="editCateForm" :rules="addCateRules" ref="editFormRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editCateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editCateForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="editVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="updateCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      // 文章分类数据
      cateList: [],
      // 文章新添弹出框的显示与隐藏
      addVisible: false,
      // 添加分类表单数据
      addCateForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加分类表单校验规则
      addCateRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      // 文章修改分类弹框的显示与隐藏
      editVisible: false,
      editCateForm: {}
    }
  },
  methods: {
    async initCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      //   console.log(res)
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    // 处理新增弹出框关闭事件
    handleAddClose () {
    // 重置添加分类的表单
      this.$refs.addCateRef.resetFields()
    },

    // 添加分类
    addCate () {
      // 1.手动校验表单合法性
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return

        // axios发送请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addCateForm)
        if (res.code !== 0) return this.$message.error(res.message)

        this.$message.success(res.message)
        // 1.关闭对话框
        this.addVisible = false
        // 2.重新获取分类数据
        this.initCateList()
      })
    },
    // 编辑分类
    async editCate (id) {
      // id为1或2的数据是不能修改的
      if (id === 1 || id === 2) return this.$message.warning('该数据不能被编辑')

      // axiox获取当前分类的数据
      // axios.get(地址,{params:参数})    get或delete
      // axios.post(地址,{参数})          post或put或patch
      // axios({
      //    method:
      //    url:
      //    data:post请求传参
      //    params:get请求传参
      // })
      const { data: res } = await this.$http.get('/my/cate/info', { params: { id } })
      if (res.code === 0) {
        // 分类数据form
        this.editCateForm = res.data
        // 编辑弹出框显示
        this.editVisible = true
      }
    },
    // 处理编辑弹出框关闭事件
    handleEditClose () {
      this.$refs.editFormRef.resetFields()
    },
    // 更新分类
    updateCate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return

        // 1.axios更新数据
        const { data: res } = await this.$http.put('/my/cate/info', this.editCateForm)

        // 2.更新失败
        if (res.code !== 0) return this.$message.error(res.message)

        // 3.更新成功
        this.$message.success(res.message)

        // 4.关闭弹出框
        this.editVisible = false

        // 5.重新加载分类数据
        this.initCateList()
      })
    },

    // 根据id删除分类
    async delCate (id) {
      if (id === 1 || id === 2) return this.$message.warning('该数据禁止删除')

      const res = await this.$confirm('此操作将永久删除该文章分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (res !== 'confirm') return

      // axios删除根据id数据
      const { data: result } = await this.$http.delete('/my/cate/del', { params: { id } })
      if (result.code !== 0) return this.$message.error(result.message)

      // 删除分类成功
      this.$message.success(result.message)
      // 重置分类数据
      this.initCateList()
    }
  },
  created () {
    this.initCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
